
import math

def get_key_value_from_file_float( key, filename, ignore_below=-1.0, ignore_up=-1.0, sep1=' ', sep2=':' ):
    key_len = len(key)
    
    values = []
    for line in open( filename, "r" ):
        cols = line.split(sep1)
        for col in cols:
            if len( col )>key_len and col[0:key_len]==key:
                v = float( col.split( sep2 )[1] )
                if ( ignore_below<0 or v>=ignore_below ) and ( ignore_up<0 or v<=ignore_up ):
                    values.append( v )

    return values

def get_key_value_from_file_int( key, filename, ignore_below=-1, ignore_up=-1, sep1=' ', sep2=':' ):
    key_len = len(key)
    
    values = []
    for line in open( filename, "r" ):
        cols = line.split(sep1)
        for col in cols:
            if len( col )>key_len and col[0:key_len]==key:
                v = int( col.split( sep2 )[1] )
                if ( ignore_below<0 or v>=ignore_below ) and ( ignore_up<0 or v<=ignore_up ):
                    values.append( v )

    return values

def mean( a ):
    s = sum( a )
    return s / float( len(a) )

def moment( a, n ):
    c = len( a )
    m = 0
    e = mean( a )
    for v in a:
        m = m + math.pow(v-e,n) / c
    return m

def variance( a ):
    return moment( a, 2 )

def standard_deviation( a ):
    return math.sqrt( variance(a) )


